#include "FileManager.h"

FileManager::FileManager(){
                           file = new fstream();
}

FileManager::~FileManager(){
                            if (isOpen())
                               this->closeFile();
                            delete file;
}

void FileManager::deleteFile(){
	 string command = "rm -f "+ this->name;
	 //cout << command << endl;
	 system(command.c_str());
}

void FileManager::openFile(string fileName, string mode){
     this->name = fileName;
     file->open((this->name).c_str(), (mode=="in")?(ios::in):(ios::out));

}
void FileManager::closeFile(){
     file->close();
}

string FileManager::readLine(){
       //char* s = new char[MAXBUFF];
       //file->getline(s, MAXBUFF);
	   string str;
       getline(*file, str);
       //string str = s;
       //delete[] s;
       return str;
}

void FileManager::writeLine(string line){
     line += "\n";
     file->write(line.c_str(), line.length());
}

string FileManager::read(long size){
       char* s = new char[size];
       file->read(s, size);
       string str = s;
       delete[] s;
       return str;
}

void FileManager::write(string str){
     file->write(str.c_str(), str.length());
}

bool FileManager::isOpen(){
     return file->is_open();
}

bool FileManager::eof(){
     return file->eof();
}
